﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CtripSOA
{
    class FileOpreate
    {
        //string root_path = @"D:/CTRIP_SOA/";
        string s = "";

        public String ReadConfig(String configPath, String configData)
        {
            StreamReader sr = new StreamReader(configPath, Encoding.Default);
            string s;
            String result = "";
            while ((s = sr.ReadLine()) != null)
            {
                //上面一行一行读。然后在里面就看你自己怎么处理了。下面是假设。    
                if (s.Contains(configData))
                {
                    string[] sArray = s.Split('=');
                    result = sArray[1];
                    break;
                }
            }
            sr.Close();
            return result;
        }

        public void WriteConfig(String configPath, String sorceData, String desData)
        {

            string con = "";
            FileStream fs = new FileStream(configPath, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);
            con = sr.ReadToEnd();
            con = con.Replace(sorceData, desData);
            sr.Close();
            fs.Close();
            FileStream fs2 = new FileStream(configPath, FileMode.Open, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs2);
            sw.WriteLine(con);
            sw.Close();
            fs2.Close();
        }

       
        //删除文件
        public Boolean deleteFile(String filepath)
        {
        	Boolean success = false;
        	
        	try {
        		 if (File.Exists(filepath))
	            {
	                //如果存在则删除
	                File.Delete(filepath);
	                success = true;
	            } else {
	            	success = false;
	            }
        	} catch (Exception e) {
        		success = false;
        	}
           
            
            return success;
        }

        //删除文件夹
        public Boolean DeleteFolder(String floderpath)
        {
        	Boolean success = false;
        	
        	try {
        		if (Directory.Exists(floderpath)) {
	            	DirectoryInfo  di = new DirectoryInfo(floderpath);
	            	di.Delete(true);
	            	success = true;
	        	} else {
	        		success = false;
	        	}
        	} catch (Exception e) {
        		success = false;
        	}
            
        	
        	return success;
        }   

        
        public void CreateFile(String filepath,String filename)
        {
          
            String fs = filepath + "\\" + filename;
          
                FileStream f = new FileStream(fs, FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(f);
                sw.Close();
                f.Close();
          
          }

        public void CreateFolder(string folderPath, string foldername)
        {
         
            String fl = folderPath + "\\" + foldername;
            
                Directory.CreateDirectory(fl);
            
        }

        public Boolean modifyFolderName(string sourceDirName, string destDirName)
        {

            Boolean rs;
            if (Directory.Exists(destDirName))
            {
                rs = false;
            }
            else
            {
                Directory.Move(sourceDirName, destDirName);
                rs = true;
            }
            return rs;
        }

        public Boolean modifyFileName(string sourceFileName, string destFileName)
        {
            Boolean rs;
            if (File.Exists(destFileName))
            {
                rs = false;
            }
            else
            {
                File.Move(sourceFileName, destFileName);
                rs = true;

            }
            return rs;

        }
        
        public string notExistFile(string filePath, string filename) {
        	string fl = filePath  + "\\" + filename;
        	int result;
        	
        	while (File.Exists(fl)) {
        		int.TryParse(Utils.searchText2Side("（", "）", filename), out result);
        		filename = "新建文件（" + (result + 1).ToString() + "）.txt";
        		fl = filePath + "\\" + filename;
        	}
        	
        	return filename;
        }
        
        public string notExistFolder(string folderPath, string foldername) {
        	
        	String fl = folderPath + "\\" + foldername;
            int result;
            
            while (Directory.Exists(fl)) {
            	int.TryParse(Utils.searchText2Side("（", "）", foldername), out result);
            	foldername = "新建文件夹（" + (result + 1).ToString() + "）";
            	fl = folderPath + "\\" + foldername;
            }
            
            return foldername;
        }
        
        public string ReadFile(string Path)
        {
            if (!System.IO.File.Exists(Path))
                s = "";
            else
            {
                StreamReader f2 = new StreamReader(Path, System.Text.Encoding.GetEncoding("GB2312"));
                s = f2.ReadToEnd();
                f2.Close();
                f2.Dispose();
            }
            return s;
        }
        
        public string ReadFile(string content, string lefside, string rightside)
        {
        	string result = "";
        	

            result = Utils.searchText2Side(lefside, rightside, content);
            return result;

        }

        public string[] ReadFileByline(string Path)
        { 
                string[] LineContent = File.ReadAllLines(@Path);
                return LineContent;     
        }
        
        public void saveFile(string content, string responseBody, string url, int type, string path) {
        	if (type == 1 || type == 2 || type == 3) {
        		string ori = this.ReadFile(path);
        		string result = "";
        		
        		RequestFileBean bean = new RequestFileBean();
        		bean.Fat = Utils.searchText2Side("<FAT>", "</FAT>", ori);
        		bean.Uat = Utils.searchText2Side("<UAT>", "</UAT>", ori);
        		bean.Pro = Utils.searchText2Side("<PRO>", "</PRO>", ori);
        		bean.Body = Utils.searchText2Side("<SCHBODY>", "</SCHBODY>", ori);
        		bean.Response = Utils.searchText2Side("<SCHRESPONSE>", "</SCHRESPONSE>", ori);
        		
        		
        		if (type == 1) {
        			bean.Fat = url;
        		} else if (type == 2) {
        			bean.Uat = url;
        		} else if (type == 3) {
        			bean.Pro = url;
        		}
        		
        		bean.Body = content;
        		bean.Response = responseBody;
        		result = bean.ToString();
        		
        		File.WriteAllText(path, result, Encoding.UTF8);
        	} else {
        		
        	}
        }
        
        public void saveAsFile(string content, string responseBody, string url, int type, string fname) {
        	if (type == 1 || type == 2 || type == 3) {
        		string result = "";
        		
        		RequestFileBean bean = new RequestFileBean();   		
        		
        		if (type == 1) {
        			bean.Fat = url;
        		} else if (type == 2) {
        			bean.Uat = url;
        		} else if (type == 3) {
        			bean.Pro = url;
        		}
        		
        		bean.Body = content;
        		bean.Response = responseBody;
        		result = bean.ToString();
        		
        		File.WriteAllText(fname, result, Encoding.UTF8);
        		
        	} else {
        		
        	}
        }
    }
}
